#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE *file;
    char msg[] = "linux so easy!";
    char buffer[50];

    // 打开文件，如果不存在则创建
    file = fopen("bite", "w");
    if (file == NULL)
    {
        perror("Error opening file");
        exit(1);
    }

    // 写入内容到文件
    if (fwrite(msg, sizeof(char), strlen(msg), file) != strlen(msg))
    {
        perror("Error writing to file");
        fclose(file);
        exit(1);
    }

    // 关闭文件流指针
    fclose(file);

    // 重新打开文件以读取
    file = fopen("bite", "r");
    if (file == NULL)
    {
        perror("Error opening file for reading");
        exit(1);
    }

    // 将文件指针重置到文件开始
    fseek(file, 0, SEEK_SET);

    // 从文件中读取内容
    size_t s = fread(buffer, sizeof(char), sizeof(buffer) - 1, file);
    if (s == 0)
    {
        perror("Error reading from file");
        fclose(file);
        exit(EXIT_FAILURE);
    }

    buffer[s] = '\0';
    printf("File msg: %s\n", buffer);

    // 关闭文件流指针
    fclose(file);

    return 0;
}